package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.labels.request.LabelsSaveRequest;
import com.fulihui.wgj.admin.api.labels.vo.LabelsVO;
import com.fulihui.wgj.admin.integration.LabelInfoServiceClient;
import com.fulihui.wgj.facade.dto.LabelsInfoDTO;
import com.fulihui.wgj.facade.request.labels.QrcodeLabelSaveRequest;
import com.fulihui.wgj.facade.service.labels.LabelInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: Levon
 * @date: 2018-07-09 15:47
 */
@Service
public class LabelInfoServiceClientImpl implements LabelInfoServiceClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(LabelInfoServiceClientImpl.class);

    @Reference(version = "1.0.0")
    private LabelInfoService labelInfoService;

    @Override
    public Result<List<LabelsVO>> queryAll() {
        SimpleResult<List<LabelsInfoDTO>> simpleResult = labelInfoService.queryAll();
        List<LabelsInfoDTO> datas = simpleResult.getData();
        if (!CollectionUtils.isEmpty(datas)) {
            List<LabelsVO> labelsVOS = BeanConv.copy(datas, LabelsVO.class);
            return JsonResultBuilder.succ(labelsVOS);
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    @Override
    public Result<String> save(LabelsSaveRequest request) {
        QrcodeLabelSaveRequest saveRequest = new QrcodeLabelSaveRequest();
        saveRequest.setLabelName(request.getName());
        SimpleResult<String> simpleResult = labelInfoService.save(saveRequest);
        LOGGER.info("labelInfoService.save simpleResult = {}", simpleResult);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            return JsonResultBuilder.succ(simpleResult.getData());
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }
}
